*This file replicates regression results for Volatility outcomes - Falsification test. 
*The code used in this file is adapted from Baker, Bloom, and Davis (2016) to generate results related to environmental policy uncertainty.

set matsize 5000
clear all
set more off
use data/final_data.dta

so year month 
merge 1:1 year month using data/modern_epu_data_clean
ren UP PU
gen eEPU=EPU/E
gen pEPU=EPU/P
gen uEPU=EPU/U
gen euEPU=EPU/EU
gen puEPU=EPU/PU
gen epEPU=EPU/EP
gen gE_P_U=(E*P*U)^(1/3)
gen aE_P_U=(E+P+U)/3
global unc="E P U EP EU PU EPU eEPU pEPU uEPU euEPU puEPU epEPU _EPU gE_P_U aE_P_U"
global lunc="lE lP lU lEP lEU lPU lEPU leEPU lpEPU luEPU leuEPU lpuEPU lepEPU l_EPU lgE_P_U laE_P_U"
foreach var in $unc {
gen l`var'=log(`var')
}
gen ym=year+(month-1)/12
gen yq=year+(floor((month-1)/3))/4
drop _m

so year month
merge m:1 year month using data/categories 
drop _m

gen quarter = 1
replace quarter = 2 if month>3
replace quarter = 3 if month>6
replace quarter =4 if month>9
lab var uncert "Combined EPU index"
lab var EPU "News only index"
collapse uncert - PCF $unc $lunc defense health fin_regulation  epu yq ym, by(year quarter)
compress
save data/epu_data, replace 
clear all

use data/volandcapex_quarterly.dta,clear

cap destring, replace
cap gen date = dofm(datadate)
cap gen quarter = quarter(date)
cap gen year = year(date)
cap gen month = month(date)

**These two lines below move from old to new data
cap merge 1:1 gvkey year quarter using  data/volandcapex_quarterly_updated.dta,update
cap tab _m
cap drop _m

***Note master data only has back until 1995
cap merge m:1 year quarter gvkey using data/all_firm_data_updated.dta
cap drop *uncert*
drop if _merge==2
drop _merge
merge m:1 year quarter using data/epu_data.dta
drop if _merge==2
drop _merge

********************Adding Volume Data
rename cusip cusip
merge m:1 cusip year quarter using data/quarterly_volume_data.dta
drop if _merge==2
drop _merge
cap order gvkey ticker* cusip* permno year quarter month datadate date datafdate
drop min_quarter 
sort gvkey year quarter
tsset gvkey period_code
gen luncert=log(uncert)

********Adding beta and 10k data
so sic3
merge m:1 sic3 using data/sic3_betas
drop if _m==2
drop _m

so gvkey
merge m:1 gvkey using data/steve10k_gvkey
winsor2 jp,replace cuts(2 98)
replace jp=log(jp)
drop if _m==2
drop _m

********************Updating/Fixing VIX
ren vix oldvix
so yq
merge m:1 yq using data/vix.dta,update
replace lvix=log(vix)

*Generate a bunch of uncertainty interactions with prefered intensity measure, giving them the same SD as the baseline
foreach var of varlist h_firm_sic_intens {
	foreach var2 of varlist $unc $lunc uncert vix luncert lvix fed fed_fore {
		qui gen `var2'_X_`var' = `var2'*`var'
	}
}

*Test out with other intensity measures 
egen sdXbaseline=sd(h_firm_sic_intens)
foreach var of varlist sic_intens belo_intens firm_intens firm_sic_intens beta jp {
	egen sdX`var'=sd(`var')
	replace `var'=`var'*sdXbaseline/sdX`var'
	qui	gen luncert_X_`var'_intens = luncert*`var'
	qui	gen lEPU_X_`var'_intens = lEPU*`var'
	qui	gen fed_X_`var'_intens = fed*`var'
}


gen lavg_vol_91  = log(avg_vol_91)
gen lend_vol_91  = log(end_vol_91)
gen lavg_vol_30  = log(avg_vol_30)
gen lend_vol_30  = log(end_vol_30)
gen lavg_vol_182  = log(avg_vol_182)
gen lend_vol_182  = log(end_vol_182)
gen lavg_vol_365  = log(avg_vol_365)
gen lend_vol_365  = log(end_vol_365)
cap gen lavg_volr  = log(avg_volr)
cap gen lend_volr  = log(end_volr)
tsset
gen l_luncert_X_h_firm_sic_intens=l.luncert_X_h_firm_sic_intens
gen f_luncert_X_h_firm_sic_intens=f.luncert_X_h_firm_sic_intens
cap label var l_luncert_X_h_firm_sic_intens "Lag Uncert*Firm Intens"
cap label var f_luncert_X_h_firm_sic_intens "Fwrd Uncert*Firm Intens"
cap label var luncert_X_h_firm_sic_intens "Log(Uncert)*Firm Intens"
cap label var fed_X_h_firm_sic_intens "Fed Purchases*Firm Intens"
cap label var fed_fore_X_h_firm_sic_intens "Frcst Fed Purchases*Firm Intens"

cap destring, replace
compress

*Missing lavg_volr in monthly data
cap gen lavg_volr=lavg_vol_91

*Cleaning of volatilty for some extreme outliers (either mistakes or stocks not normally traded)
cap gen ratio_91=avg_vol_91/avg_volr
cap replace lavg_vol_91=. if ratio_91<3|ratio_91>100
foreach var in lavg_vol_30 lavg_vol_91 lavg_vol_182 lavg_volr {
su `var',de
replace `var'=r(p1) if `var'<r(p1)&`var'~=.
replace `var'=r(p99) if `var'>r(p99)&`var'~=.
}

*For firm size interacting with env
gen sizeemp=emp
replace sizeemp=round(1000*sizeemp)

*For weighting purposes useful
egen memp=mean(emp)
replace emp=memp if emp==.
egen mtemp=mean(emp),by(ticker_code)
replace emp=round(1000*mtemp) 
replace emp=1 if emp==0

*Drop without volume
drop if volume==0|volume==.

*Can get volume results if censor/windosrize on volume
gen lvolume=log(1+volume)
su lvolume,de
replace lvolume=. if lvolume<r(p1)
replace lvolume=r(p99) if lvolume>r(p99)&lvolume~=.

compress
cap log close
cap log using "volatility",t replace
drop _m
merge m:1 year quarter using data/04_Env_Policy_indices_qtr

gen SIC2=int(sic4/100)
gen env_firms=1 if inlist(sic3, 104, 109, 122, 131, 138, 241, 242, 491, 492) | inlist(SIC2, 1, 2, 37)
replace env_firms=0 if env_firms==.
gen def_firms = inlist(sic3, 348, 381, 871) 
gen health_firms=(sic3>=800&sic3<=809)
gen fin_firms= (sic3>=600&sic3<=679) 

foreach var in  EPU_NB EP_NB {
gen l`var'=log(1+`var')
}
foreach var in EPU_NB EP_NB {
foreach firms in env_firms def_firms health_firms fin_firms{
gen l`var'_X_`firms' = l`var'*`firms'
}
}

****** Falsification test
reghdfe lavg_vol_30 lEPU_NB_X_env lEP_NB_X_env lEPU_NB_X_def lEP_NB_X_def lEPU_NB_X_health lEP_NB_X_health lEPU_NB_X_fin lEP_NB_X_fin lEPU_X_h_firm_sic_intens fed_X_h_firm_sic_intens   [aw=emp] , ab(ticker_code period_code) vce(cl ticker_code)
